package com.woniuxy.common.utils;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yellow Doctor
 * @date 2025-03-20
 * @desc 蜗牛创想：www.woniuxy.com
 *   hutool 操作jwt
 *
 *     1、创建jwt
 *
 *     2、校验
 *
 *     3、解析
 *
 */
public class JwTTest {

    public static void main(String[] args) {

        byte [] KEY = "1234".getBytes();

        //1、创建jwt
        Map<String,Object> payload = new HashMap<>();
        payload.put("phone","1856623");
        payload.put("userId","12345");

        String token = JWTUtil.createToken(payload, KEY);
        System.out.println("token = " + token);

        //2、校验
        boolean verify = JWTUtil.verify(token, KEY);
        System.out.println("verify = " + verify);

        //3、解析
        JWT jwt = JWTUtil.parseToken(token);

        String phone = jwt.getPayload("phone").toString();
        System.out.println("phone = " + phone);

        String userId = jwt.getPayload("userId").toString();
        System.out.println("userId = " + userId);


    }

}
